
/*
  ADCNano3
  Reads on TWI/I2C request analog value
  For Nano3, clock @ 16 MHz
  Mac OS X device: /dev/tty.usbserial-A900FYDY

  This example code is in the public domain.
   2013-12-06 RR
 */

#include <Wire.h>
#include "stdio.h"
//#include "stdlib.h"
 
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;            // on ATtiny25 (D)4, pin 3

int analogPin = 3;       // AD8307

int aval_i = 0;          // analog value input
int aval_s = 0;          // analog sum
int aval = 0;            // analog value
int achanin = 0;         // analog channel in
char buff[30];           // for debug sprintf

#define avg_num 8        // average number, max 30
int avg_cnt = 0;         // average count

//*********************   send debug string to UART1
void com_print(char *daten) {
    while( *daten) { Serial.write(*daten++);         }
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
    while(1 < Wire.available()) {  // loop through all but the last
        char c = Wire.read();      // receive byte as a character
        //Serial.println(c);         // print the character
    }
    achanin = Wire.read();      // receive byte as an integer
    //Serial.println(achanin, DEC);    // print the integer
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
// if you do not use the "length" parameter, you could not trasmit "00".
void requestEvent() {
    byte bytes[2];
    bytes[0] = aval >> 8;    // upper byte
    bytes[1] = aval & 0xFF;  // lower byte
    Wire.write(bytes, 2); 
}
    
// the setup routine runs once when you press reset:
void setup() { 
    Serial.begin(38400);       // for debug only
    Wire.begin(5);                 // slave address
    Wire.onReceive(receiveEvent);  // register event
    Wire.onRequest(requestEvent);  // register event
    pinMode(led, OUTPUT);      // initialize the digital pin as an output.
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(200);                // wait for 100 ms
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(100); 
    //Serial.println("Hello");
}

// the loop routine runs over and over again forever:
void loop() {
    //int pause_ms = 1000;

    // start A/D convertion with a I2C master request
    if (achanin == 2 || achanin ==3) {
        analogPin = achanin;
        achanin = 0;          // ACK master command
        // average voltage
        aval_s = 0;
        for (avg_cnt = 0; avg_cnt < avg_num; avg_cnt++) {
            aval_i = analogRead(analogPin);    // read the input pin
            aval_s += aval_i;
        }
        aval = aval_s / avg_num;
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage)
        digitalWrite(led, LOW);    // EOC for convewrsion time measurement
        //Serial.println(aval_3, DEC); 
        //sprintf(buff,"2 %d, 3 %d\r\n", aval_2, aval_3);
        //com_print(buff);
    }
    //delay(pause_ms);
}

